package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.UserLonLat;
import com.api.shopapi.service.UserLonLatService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;

//用户经纬度

@RestController
@RequestMapping("UserLonLat")
public class UserLonLatController extends BaseController{

    @Autowired
    @Qualifier("UserLonLatServiceImplOne")
    private UserLonLatService userLonLatService;

    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");
            String lon = jsonObject.getString("lon");
            String lat = jsonObject.getString("lat");


            PreCheck preCheck=checkSignFrontInterface(timeStr,sign,"/UserLonLat/Add");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            UserLonLat userLonLat=new UserLonLat();
            userLonLat.setUserLonLatCode(UUID.randomUUID().toString());
            userLonLat.setCreateTime(new Date());
            userLonLat.setUserCode(userCode);
            userLonLat.setLon(lon);
            userLonLat.setLat(lat);
            userLonLatService.insert(userLonLat);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/UserLonLat/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }



    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userLonLatCode = jsonObject.getString("userLonLatCode");
            String lon = jsonObject.getString("lon");
            String lat = jsonObject.getString("lat");


            PreCheck preCheck=checkSignFrontInterface(timeStr,sign,"/UserLonLat/Edit");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            UserLonLat userLonLat = userLonLatService.getByUserLonLatCode(userLonLatCode);
            userLonLat.setLon(lon);
            userLonLat.setLat(lat);
            userLonLatService.update(userLonLat);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/UserLonLat/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Delete",method = RequestMethod.POST)
    public MyResult Delete(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userLonLatCode = jsonObject.getString("userLonLatCode");


            PreCheck preCheck=checkSignFrontInterface(timeStr,sign,"/UserLonLat/Delete");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }
            userLonLatService.delete(userLonLatCode);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/UserLonLat/Delete",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "ListTime",method = RequestMethod.POST)
    public MyResult ListTime(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            Date startTime = jsonObject.getDate("startTime");
            Date endTime = jsonObject.getDate("endTime");
            String userCode = jsonObject.getString("userCode");

            PreCheck preCheck=checkSignFrontInterface(timeStr,sign,"/UserLonLat/List");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            List<UserLonLat> userLonLatList = userLonLatService.getListBySpanTime(userCode,startTime,endTime);

            return success(userLonLatList);
        } catch (Exception e) {
            insertError("/UserLonLat/List",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "ListAfterTime",method = RequestMethod.POST)
    public MyResult ListAfterTime(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            Date lastTime = jsonObject.getDate("lastTime");
            String userCode = jsonObject.getString("userCode");

            PreCheck preCheck=checkSignFrontInterface(timeStr,sign,"/UserLonLat/List");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            List<UserLonLat> userLonLatList = userLonLatService.getListAfterTime(userCode,lastTime);

            return success(userLonLatList);
        } catch (Exception e) {
            insertError("/UserLonLat/List",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }



    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");


            PreCheck preCheck=checkSignFrontInterface(timeStr,sign,"/UserLonLat/Page");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }
            PageInfo<UserLonLat> userLonLatPageInfo = userLonLatService.getPageList(pageIndex, pageSize);
            return success(userLonLatPageInfo);
        } catch (Exception e) {
            insertError("/UserLonLat/Page",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }









}
